package JZ;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @Classname JZ32I
 * @Description // https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/ 树 一次ac
 * @Date 2021/4/21 14:49
 * @Created by yin
 */
public class JZ32I {
    public int[] levelOrder(TreeNode root) {
        if(root==null) return new int[0];
        LinkedList<TreeNode> queue = new LinkedList<>();
        ArrayList<Integer> list = new ArrayList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            list.add(node.val);
            if(node.left !=null) queue.offer(node.left);
            if(node.right !=null) queue.offer(node.right);
        }
        int[] ans = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ans[i] = list.get(i);
        }
        return ans;
    }
}
